package com.zds.leetcode;

public class q_622 {
    public static void main(String[] args) {
        MyCircularQueue obj = new MyCircularQueue(3);
        System.out.println("add result " + obj.enQueue(0));
        System.out.println("add result " + obj.enQueue(4));
        System.out.println("add result " + obj.enQueue(6));
        System.out.println("add result " + obj.enQueue(3));
        System.out.println(obj.Rear());
        System.out.println("del result " + obj.deQueue());
        System.out.println("del result " + obj.deQueue());
        System.out.println(obj.Front());

//        System.out.println("add result" + obj.enQueue(5));
//        System.out.println("del result " + obj.deQueue());
//        System.out.println(obj.size);
//        System.out.println(obj.Front());
    }

    static class MyCircularQueue {
        int[] arr;
        int maxSize, he, ta;

        public MyCircularQueue(int k) {
            arr = new int[k];
            maxSize = k;
        }

        public boolean enQueue(int value) {
            // 判断超长
            if(isFull()) {
                return false;
            }
            arr[ta % maxSize] = value;
            ta++;
            return true;
        }

        public boolean deQueue() {
            // 判断空
            if(isEmpty()) {
                return false;
            }
            he++;
            // 删除队尾元素
            return true;
        }

        public int Rear() {
            if(isEmpty()) {
                return -1;
            }
            return arr[he % maxSize];
        }

        public int Front() {
            if(isEmpty()) {
                return -1;
            }
            return arr[(ta - 1) % maxSize];
        }

        public boolean isEmpty() {
            return he == ta;
        }

        public boolean isFull() {
            return ta - he == maxSize;
        }
    }
}
